package com.fuda.trade.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
sealed class TradeOrderData(  ) {
    @Serializable
    data class Buy(
        /// 证券名称，采自《国信证券》持仓记录详情
        @SerialName("security_name")val securityName: String,
        /// 证券代码，采自《国信证券》持仓记录详情
        @SerialName("security_code")val securityCode: String,
        @SerialName("pricing_type")val pricingType: PricingType,
        val price: Double,
        val volume: Int,
        val desc: String,
        @SerialName("is_auto") var isAuto:Boolean?=false
       ) : TradeOrderData()

    @Serializable
    data class Sell(
        /// 证券名称，采自《国信证券》持仓记录详情
        @SerialName("security_name")val securityName: String,
        /// 证券代码，采自《国信证券》持仓记录详情
        @SerialName("security_code")val securityCode: String,
        @SerialName("pricing_type")val pricingType: PricingType,
        val price: Double,
        val volume: Int,
        val desc: String,
        @SerialName("is_auto") var isAuto:Boolean?=false
       ) : TradeOrderData()

    enum class PricingType(val description: String) {
        Limit("限价委托"),
        Opp("对方最优价格")
    }

    companion object {
        private val json = Json { encodeDefaults = true }

        fun serialize(tradeOrderData: TradeOrderData): String {
            return json.encodeToString(tradeOrderData)
        }

        fun deserialize(json: String): TradeOrderData {
            return Companion.json.decodeFromString(json)
        }
    }
}
